﻿using System;
using System.Collections.Generic;
using System.Web.Mvc;
using NHibernate;
using SimpleScrum.Areas.Scrum.Models;
using SimpleScrum.Core.Entities;
using SimpleScrum.Helpers;
using SimpleScrum.Utils;

namespace SimpleScrum.Areas.Scrum.Controllers
{
    public class UserStoryController : Controller
    {
        public UserStoryModel ObjUserStory;
        public UserStoryTaskModel ObjUserStoryTask;
        private Project _project = new ProjectModel().GetProjectById(Client.Instance.CurrentProjectId);
        private ISession _session = NHibernateHelper.GetSession();

        public UserStoryController()
        {
            ObjUserStory = new UserStoryModel();
            ObjUserStoryTask = new UserStoryTaskModel();
        }

        // 
        // GET: /UserStory/ 
        [HttpGet]
        public ActionResult Index(Guid projectId)
        {
            IList<UserStory> userStorys = ObjUserStory.GetUserStorys(projectId);
            return View(userStorys);
        }

        // 
        // GET: /UserStory/Details/
        [HttpGet]
        public ActionResult Details(Guid id)
        {
            var model = new UserStoryTasksModel()
                {
                    UserStoryTaskList = ObjUserStoryTask.GetTasks(id),
                    UserStoryInfo = ObjUserStory.GetUserStoryById(id)
                };
            
            return View(model);
        }

        // 
        // GET: /UserStory/Create 
        [HttpGet]
        public ActionResult New()
        {
            var userStory = new UserStory();

            return View(userStory);
        }

        // 
        // POST: /UserStory/Create 
        [HttpPost]
        public ActionResult New(UserStory userStory)
        {
            try
            {
                userStory.Project = _project;
                ObjUserStory.NewUserStory(userStory);

                return RedirectToAction("Index", "UserStory", new {projectId = Client.Instance.CurrentProjectId});
                // return Index();
            }
            catch
            {
                return View();
            }
        }

        // 
        // GET: /UserStory/Edit/5 
        [HttpGet]
        public ActionResult Edit(Guid id)
        {
            UserStory userStory = ObjUserStory.GetUserStoryById(id);
            return View(userStory);
        }

        // 
        // POST: /UserStory/Edit/5 
        [HttpPost]
        public ActionResult Edit(Guid id, UserStory userStory)
        {
            try
            {
                ObjUserStory.UpdateUserStory(userStory);
                return RedirectToAction("Index", "UserStory", new {projectId = Client.Instance.CurrentProjectId});
            }
            catch
            {
                return View(userStory);
            }
        }

        // 
        // GET: /UserStory/Delete/5 
        [HttpGet]
        public ActionResult Delete(Guid id)
        {
            UserStory userStory = ObjUserStory.GetUserStoryById(id);
            return View(userStory);
        }

        // 
        // POST: /UserStory/Delete/5 
        [HttpPost]
        public ActionResult Delete(Guid id, FormCollection collection)
        {
            try
            {
                UserStory userStory = ObjUserStory.GetUserStoryById(id);
                ObjUserStory.DeleteUserStory(userStory);
                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                return View();
            }
        }
    }
}